package cn.iocoder.yudao.module.signature.controller.app.signinfo;

import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.signature.controller.app.signinfo.vo.ApplicationSignInfoPageReqVO;
import cn.iocoder.yudao.module.signature.controller.app.signinfo.vo.ApplicationSignInfoRespVO;
import cn.iocoder.yudao.module.signature.controller.app.signinfo.vo.ApplicationSignInfoSaveReqVO;
import cn.iocoder.yudao.module.signature.dal.dataobject.signinfo.ApplicationSignInfoDO;
import cn.iocoder.yudao.module.signature.service.signinfo.ApplicationSignInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "app - 应用签名记录")
@RestController
@RequestMapping("/signature/application-sign-info")
@Validated
public class AppApplicationSignInfoController {

    @Resource
    private ApplicationSignInfoService applicationSignInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建应用签名记录")
    @PreAuthenticated
    public CommonResult<Long> createApplicationSignInfo(@Valid @RequestBody ApplicationSignInfoSaveReqVO createReqVO) throws Exception {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Assert.notNull(loginUser, "用户未登录");
        createReqVO.setUserId(loginUser.getId());
        return success(applicationSignInfoService.createApplicationSignInfo(createReqVO));
    }

    @GetMapping("/checkSignComplete")
    @Operation(summary = "检查签名是否完成")
    public CommonResult<Integer> lockTimeCheck(@RequestParam("checkUrl") String checkUrl) throws Exception{
        return success(applicationSignInfoService.checkSignComplete(checkUrl));
    }

    @PostMapping("/signCard")
    @Operation(summary = "卡密安装应用")
    public CommonResult<ApplicationSignInfoDO> signCard(@Valid @RequestBody ApplicationSignInfoSaveReqVO createReqVO) throws Exception {
        return success(applicationSignInfoService.signCard(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新应用签名记录")
    @PreAuthenticated
    public CommonResult<Boolean> updateApplicationSignInfo(@Valid @RequestBody ApplicationSignInfoSaveReqVO updateReqVO) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Assert.notNull(loginUser, "用户未登录");
        updateReqVO.setUserId(loginUser.getId());
        applicationSignInfoService.updateApplicationSignInfo(updateReqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除应用签名记录")
    @PreAuthenticated
    public CommonResult<Boolean> deleteApplicationSignInfo(@RequestBody List<Long> ids) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Assert.notNull(loginUser, "用户未登录");
        applicationSignInfoService.deleteApplicationSignInfo(ids,loginUser.getId());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得应用签名记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthenticated
    public CommonResult<ApplicationSignInfoRespVO> getApplicationSignInfo(@RequestParam("id") Long id) {
        ApplicationSignInfoDO applicationSignInfo = applicationSignInfoService.getApplicationSignInfo(id);
        return success(BeanUtils.toBean(applicationSignInfo, ApplicationSignInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得应用签名记录分页")
    @PreAuthenticated
    public CommonResult<PageResult<ApplicationSignInfoRespVO>> getApplicationSignInfoPage(@Valid ApplicationSignInfoPageReqVO pageReqVO) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Assert.notNull(loginUser, "用户未登录");
        pageReqVO.setUserId(loginUser.getId());
        PageResult<ApplicationSignInfoDO> pageResult = applicationSignInfoService.getApplicationSignInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ApplicationSignInfoRespVO.class));
    }

    @GetMapping("/listRecentData")
    @Operation(summary = "获得应用签名记录近5分钟内的数据")
    @PreAuthenticated
    public CommonResult<List<ApplicationSignInfoRespVO>> listRecentData() {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Assert.notNull(loginUser, "用户未登录");
        List<ApplicationSignInfoDO> list = applicationSignInfoService.listRecentData(loginUser.getId());
        return success(BeanUtils.toBean(list, ApplicationSignInfoRespVO.class));
    }


    @PostMapping("/excuteSignTaskTest")
    @Operation(summary = "执行签名任务")
    public CommonResult<Boolean> excuteSignTaskTest(@RequestParam("id") Long id) throws Exception {
        applicationSignInfoService.excuteSignTaskTest(id);
        return success(true);
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出应用签名记录 Excel")
//    @PreAuthorize("@ss.hasPermission('signature:application-sign-info:export')")
//    @PreAuthenticated
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportApplicationSignInfoExcel(@Valid ApplicationSignInfoPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<ApplicationSignInfoDO> list = applicationSignInfoService.getApplicationSignInfoPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "应用签名记录.xls", "数据", ApplicationSignInfoRespVO.class,
//                        BeanUtils.toBean(list, ApplicationSignInfoRespVO.class));
//    }

    @PostMapping("/testPackaging")
    public CommonResult<String> testPackaging(@RequestParam("file") MultipartFile file) throws Exception {
        return CommonResult.success(applicationSignInfoService.testPackaging(file)) ;
    }
    /**
     * 时间锁检查
     * @param id
     * @return
     */
    @GetMapping("/lockTimeCheck")
    @Operation(summary = "时间锁检查")
    public CommonResult lockTimeCheck(@RequestParam("id") Long id) {
        return applicationSignInfoService.lockTimeCheck(id);
    }
}